*********************************************************************************
** Health Service Provision Audit Analysis
*********************************************************************************

	capture log close
	clear all
	macro drop _all
	set more off
	version 14.2
	set matsize 11000

********************************************************************************
 *Set working directory 

capture cd "replication_APSR"
	
********************************************************************************

use "HC_audits.dta"

********************************************************************************
*** Dependent variables	
	*1* Number of new staff in previous 3 months
	*2* Number of applications to DHO for hiring/firing in previous 3 months
	*3* Number of visits from councilors in past 6 months
	*4* Percentage of staff not present at the hospital (Absenteeism)
	*5* Dummy for having modern lighting
	*6* Dummy for having stoves for sterilization and cooking
	*7* Number of outreach campaigns in the past 3 months
	*8* Categorical for the # of staff meetings held in the past six months
********************************************************************************

********************************************************
* Define covariates
******************************************************
encode dist_name, gen(distid)

* define competitive moderator equal 1 if at least one councilors is in a competitive constituency 
gen competitive=0
replace competitive=1 if rc_competitive==1 | swc_competitive==1
tab competitive

gen pop1000 = subc_totalpop/1000
lab var pop1000 "Subcounty population (thousands)"

* define covariates
gl covs rc_NRM swc_NRM rc_Edu swc_Edu rc_FirstTerm swc_FirstTerm rc_educ swc_educ rc_cterms swc_cterms rc_polexpterms swc_polexpterms rc_counterms swc_counterms pop1000 subc_hhi 

	* assign mean values of control to missing values of covariates (Lin, green and Coppock, 2015))
	foreach y in $covs {
	 egen `y'_mean=mean(`y')
	 gen `y'_miss = `y'==.
	 replace `y' = `y'_mean if `y'==.
	 }

gl controls rc_NRM swc_NRM rc_Edu swc_Edu  rc_FirstTerm swc_FirstTerm pop1000 pop1000_miss subc_hhi rc_NRM_miss swc_NRM_miss rc_FirstTerm_miss swc_FirstTerm_miss subc_hhi_miss	 
su $controls

encode dist_name, gen(distrid)

* define sample
gen sample =0
replace sample=1 if rc_NRM!=. & swc_NRM!=. & rc_Edu!=. & swc_Edu!=. & c.pop1000!=. & c.subc_hhi!=. & c.subc_poverty!=.

**************
* DV
**************

gl DVs Absenteeism Reg_Counc_Visits DHO_requests New_Employee StaffMeeting Outreach 
su $DVs

********************************************************
* unconditional treatment effects
********************************************************
set more off

forvalues i=0(1)1{
foreach y in $DVs{
	
reghdfe `y' i.ID if sample==1 & phase==`i', abs(distid) vce(r)
	estadd local control ""
	estadd local district "X"
	estimate store `y'_p`i'_m1

reghdfe `y' i.ID $controls if sample==1 & phase==`i', abs(distid) vce(r)
	estadd local control "X"
	estadd local district "X"
	estimate store `y'_p`i'_m2 	
	margins , dydx(ID) post

********************************************************
* effects conditional on competitiveness
******************************************************
* Binary

reghdfe `y' ID##competitive if sample==1 & phase==`i', abs(distid) vce(r)
	estadd local control ""
	estadd local district "X"
	estimate store `y'_p`i'_m3
	
reghdfe `y' ID##competitive $controls if sample==1 & phase==`i', abs(distid) vce(r)
	estadd local control "X"
	estadd local district "X"
	estimate store `y'_p`i'_m4
	margins, dydx(ID) over(competitive) post
	estimate restore `y'_p`i'_m4
	margins r.ID, over(r.competitive) post

	}
	}
	

*Table 26 oline appendix midline panel
# delimit ; 
esttab  Absenteeism_p0_m1 Absenteeism_p0_m2  
		DHO_requests_p0_m1 DHO_requests_p0_m2 
		New_Employee_p0_m1 New_Employee_p0_m2 
		StaffMeeting_p0_m1 StaffMeeting_p0_m2 
		Outreach_p0_m1 Outreach_p0_m2 
using "tables/HealthUnconditional_phase0.tex",
		keep(1.ID  )
	    order(1.ID )
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01) 					
		mgroups("\textbf{Absenteeism}" "\textbf{DHO applications}"
		"\textbf{New Employees}" "\textbf{Outreach campaigns}" "\textbf{Staff meetings}", pattern(1 0 1 0 1 0 1 0 1 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		varlabels(1.ID "ID")
		stats(district control N, labels("District FE" "Controls" "N")
		fmt(0 0)) collabels(none) label booktabs nonotes
		replace;
		#delimit cr

*Table 26 endline panel
# delimit ; 
esttab  Absenteeism_p1_m1 Absenteeism_p1_m2  
		DHO_requests_p1_m1 DHO_requests_p1_m2 
		New_Employee_p1_m1 New_Employee_p1_m2 
		StaffMeeting_p1_m1 StaffMeeting_p1_m2 
		Outreach_p1_m1 Outreach_p1_m2 
using "tables/HealthUnconditional_phase1.tex",
		keep(1.ID  )
	    order(1.ID )
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01) 					
		mgroups("\textbf{Absenteeism}" "\textbf{DHO applications}"
		"\textbf{New Employees}" "\textbf{Outreach campaigns}" "\textbf{Staff meetings}", pattern(1 0 1 0 1 0 1 0 1 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		varlabels(1.ID "ID")
		stats(district control N, labels("District FE" "Controls" "N")
		fmt(0 0)) collabels(none) label booktabs nonotes
		replace;
		#delimit cr
		# delimit ; 

*Table 28 midline panel
# delimit ;		
esttab  Absenteeism_p0_m3 Absenteeism_p0_m4  
		DHO_requests_p0_m3 DHO_requests_p0_m4 
		New_Employee_p0_m3 New_Employee_p0_m4 
		StaffMeeting_p0_m3 StaffMeeting_p0_m4 
		Outreach_p0_m3 Outreach_p0_m4 
using "tables/HealthConditional_phase0.tex",
		keep(1.ID 1.competitive 1.ID#1.competitive )
	    order(1.ID 1.competitive 1.ID#1.competitive )
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01) 					
		mgroups("\textbf{Absenteeism}" "\textbf{DHO applications}"
		"\textbf{New Employees}" "\textbf{Outreach campaigns}" "\textbf{Staff meetings}", pattern(1 0 1 0 1 0 1 0 1 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		varlabels(1.ID "ID" 1.competitive "Competitive"
				  1.ID#1.phase "\$ID\times Competitive$")
		stats(district control N, labels("District FE" "Controls" "N")
		fmt(0 0)) collabels(none) label booktabs nonotes
		replace;
		#delimit cr

*Table 28 endline panel
# delimit ;		
esttab  Absenteeism_p1_m3 Absenteeism_p1_m4  
		DHO_requests_p1_m3 DHO_requests_p1_m4 
		New_Employee_p1_m3 New_Employee_p1_m4 
		StaffMeeting_p1_m3 StaffMeeting_p1_m4 
		Outreach_p1_m3 Outreach_p1_m4 
using "tables/HealthConditional_phase1.tex",
		keep(1.ID 1.competitive 1.ID#1.competitive )
	    order(1.ID 1.competitive 1.ID#1.competitive )
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01) 					
		mgroups("\textbf{Absenteeism}" "\textbf{DHO applications}"
		"\textbf{New Employees}" "\textbf{Outreach campaigns}" "\textbf{Staff meetings}", pattern(1 0 1 0 1 0 1 0 1 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		varlabels(1.ID "ID" 1.competitive "Competitive"
				  1.ID#1.phase "\$ID\times Competitive$")
		stats(district control N, labels("District FE" "Controls" "N")
		fmt(0 0)) collabels(none) label booktabs nonotes
		replace;
		#delimit cr		
		
